package com.platform.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.lang.Validator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.WarmClassEntity;
import com.platform.service.WarmClassService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 暖课课程Controller
 *
 * @author czj
 * @email 729618941@qq.com
 * @date 2018-11-01 16:50:05
 */
@RestController
@RequestMapping("warmclass")
public class WarmClassController {
    @Autowired
    private WarmClassService warmClassService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("warmclass:list")
    public R list(@RequestParam Map<String, Object> params) {

        //默认按照排序号排序
        if (Validator.isEmpty(params.get("sidx"))) {
            params.put("sidx", "sort_order");
            params.put("order", "desc");
        }

        //查询列表数据
        Query query = new Query(params);

        List<WarmClassEntity> warmClassList = warmClassService.queryList(query);
        if (warmClassList.size() > 0) {
            for (WarmClassEntity warmClassEntity : warmClassList) {
                warmClassEntity.setWxAppLink("../nuankeDetail/nuankeDetail?warmClassId=" + warmClassEntity.getId());
            }
        }

        int total = warmClassService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(warmClassList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("warmclass:info")
    public R info(@PathVariable("id") Integer id) {
        WarmClassEntity warmClass = warmClassService.queryObject(id);

        return R.ok().put("warmClass", warmClass);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("warmclass:save")
    public R save(@RequestBody WarmClassEntity warmClass) {
        warmClass.setAddTime(new Date());
        warmClass.setViewCount(0);
        warmClass.setIsDelete(0);
        warmClassService.save(warmClass);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("warmclass:update")
    public R update(@RequestBody WarmClassEntity warmClass) {
        warmClass.setUpdateTime(new Date());
        warmClassService.update(warmClass);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("warmclass:delete")
    public R delete(@RequestBody Integer[] ids) {
        warmClassService.updateBatch(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<WarmClassEntity> list = warmClassService.queryList(params);

        return R.ok().put("list", list);
    }
}
